package org.kailu.book.DAO;

import java.util.List;

import org.hibernate.SessionFactory;
import org.kailu.book.entity.DepartmentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DepartmentDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	public void save(DepartmentEntity departmentEntity) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(departmentEntity);
	}
	
	@SuppressWarnings("unchecked")
	public List<DepartmentEntity> getAll() {
		return this.sessionFactory.getCurrentSession().createCriteria(DepartmentEntity.class).list();
	}
	
	public DepartmentEntity getById(int id) {
		return (DepartmentEntity) this.sessionFactory.getCurrentSession().get(DepartmentEntity.class, id);
	}
	
	public void delete(int id) {
		DepartmentEntity departmentEntity = (DepartmentEntity) this.sessionFactory.getCurrentSession().get(DepartmentEntity.class, id);
		
		if (departmentEntity != null) {
			this.sessionFactory.getCurrentSession().delete(departmentEntity);
		}
	}
	
}
